/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
$(function(){
    var _tabPanel=$("#main");
//    freshAD();
    $(".easyui-accordion").find("a[link]").each(function(){
        $(this).click(function(){
            var url=$(this).attr("link");
            var title=$(this).html();
            if(url!=="#"&&url!==""){
                if(!_tabPanel.tabs('exists',title)){
                    _tabPanel.tabs('add',{
                        title:title,
                        content:'<iframe src="'+url+'" style="padding:0;margin:0;border:0;width:100%;height:100%;"></iframe>',
                        closable:true
                    });
                    bindfresh(title);
                }else{
                    _tabPanel.tabs('select',title);
                }
            }
        });
    });//end each
    window.document.addTab=function(url,title){
        var _tabPanel=$("#main");
        if(!_tabPanel.tabs('exists',title)){
            _tabPanel.tabs('add',{
                title:title,
                content:'<iframe src="'+url+'" style="padding:0;margin:0;border:0;width:100%;height:100%;"></iframe>',
                closable:true
            });
            bindfresh(title);
        }else{
            _tabPanel.tabs('select',title);
        }
    }//end window.document.addTab
        
});//end ready
function freshAD(){
    var AdHtml="<font color='red'>现在时间："+new Date().format("hh:mm:ss")+"&nbsp;&nbsp;今天："+getChineseCalendar()+"</font>";
    $("#opt_info").panel({
        title:'AdHtml'
    });
    setTimeout("freshAD()",500);
}
function goHome(){
    $("#main").tabs('select',"后台首页");
}
function wopen(url){
    window.open (url,'newwindow','height=800,width=1100,toolbar=no,menubar=no,scrollbars=no, resizable=no,location=no, status=no')
}
function bindfresh(title){
    /*双击刷新TAB选项卡*/
    $(".tabs-inner").dblclick(function(){
        var _ctab=$('#main').tabs('getTab',title);
        var html=_ctab.html();
        _ctab.html(html);
    });//end $(".tabs-inner").dblclick
}

function clearForm(){
    $('#ff').form('clear');
}
function dgfind(){
    var data = $('#ff').form('serialize');
    $('#dg').datagrid('load',data);
}
Array.prototype.in_array = function(e) 
{ 
    for(i=0;i<this.length;i++)
    {
        if(this[i] == e)
        return true;
    }
    return false;
}


$.extend($.fn.validatebox.defaults.rules, {  
        letterName: {  
            validator: function(value, param){  
                if(value.length>30){
                    alert('不要大于30位');
                    return false;
                }
                var regex = /^([a-zA-Z]|\d|_)*$/;
                return regex.test(value);
            },  
            message: '请输入字母数字下划线，且不要大于30位！'  
        },
        minLength: {  
            validator: function(value, param){  
                if(value.length<6){
                    return false;
                }else{
                    return true;
                }
            },  
            message: '请输入至少6位密码！'  
        },
        contact: {  
            validator: function(value, param){  
                if(value.length>16){
                    alert('不要大于16位');
                    return false;
                }
                var regex = /^(\d|-{0,1})*$/;
                return regex.test(value);
            },  
            message: '请输入数字，中间可以有一位“-”！'  
        },
        equalTo: {
            validator:function(value,param){
                return $(param[0]).val() == value;
            },
            message:'字段不匹配'
        }
    });
        
$.extend($.fn.form.methods, {  
        serialize: function(jq){  
                var arrayValue = $(jq[0]).serializeArray();
                var json = {};
                $.each(arrayValue, function() {
                        var item = this;
                        if (json[item["name"]]) {
                                json[item["name"]] = json[item["name"]] + "," + item["value"];
                        } else {
                                json[item["name"]] = item["value"];
                        }
                });
                return json; 
        },
        getValue:function(jq,name){  
                var jsonValue = $(jq[0]).form("serialize");
                return jsonValue[name]; 
        },
        setValue:function(jq,data){
                return jq.each(function () {
                            $(this).form("load",data);
                });
        }
});

$.extend($.fn.datagrid.defaults.editors, {  
    text: {  
        init: function(container, options){  
            var input = $('<input type="text" class="easyui-validatebox" />').appendTo(container);  
            input.validatebox(options);
            return input;  
        },  
        getValue: function(target){  
            return $(target).val();  
        },  
        setValue: function(target, value){  
            $(target).val(value);  
        },  
        resize: function(target, width, options){  
            var input = $(target);  
            if ($.boxModel == true){  
                input.width(width - (input.outerWidth() - input.width()));  
            } else {  
                input.width(width);  
            } 
        }  
    },
    upload: {  
        init: function(container, options){  
            var input = $('<input type="file" class="datagrid-editable-input">').appendTo(container);  
            return input;  
        },  
        getValue: function(target){  
            return $(target).val();  
        },  
        setValue: function(target, value){  
            $(target).val(value);  
        },  
        resize: function(target, width){  
            var input = $(target);  
            if ($.boxModel == true){  
                input.width(width - (input.outerWidth() - input.width()));  
            } else {  
                input.width(width);  
            }  
        }  
    },
    search:{
        init: function(container, options){  
            var input = $('<a href="#" class="easyui-linkbutton" iconCls="icon-search" onclick="$(\'#materialsDiv\').dialog(\'open\')">选取</a>').appendTo(container);  
            return input;  
        },  
        getValue: function(target){  
            return $(target).val();  
        },  
        setValue: function(target, value){  
            $(target).val(value);  
        },  
        resize: function(target, width){  
            var input = $(target);  
            if ($.boxModel == true){  
                input.width(width - (input.outerWidth() - input.width()));  
            } else {  
                input.width(width);  
            }  
        }
    }
});
(function($){
       $.fn.extend({
              serializeObject:function(){
                     if(this.length>1){
                            return false;
                     }
                     var arr=this.serializeArray();
                     var obj=new Object;
                     $.each(arr,function(k,v){
                            obj[v.name]=v.value;
                     });
                     return obj;
              }
       });
})(jQuery);

Array.prototype.in_array = function(e) 
{ 
    for(i=0;i<this.length;i++)
    {
        if(this[i] == e)
        return true;
    }
    return false;
}